package com.letoken.platform.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.app.service.AppExReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.pub.util.MD5Util;
import com.letoken.platform.repo.app.redis.AppExReportMd5Redis;
import com.letoken.platform.repo.app.req.ex.ExReportReq;
import com.letoken.platform.repo.repo.dao.AppExReportMapper;
import com.letoken.platform.repo.repo.po.AppExReport;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-06-07
 */
@Service
@Log4j2
public class AppExReportServiceImpl extends ServiceImpl<AppExReportMapper, AppExReport> implements AppExReportService {

    @Override
    public void report(ExReportReq req) {
        String jsonStr = JSONObject.toJSONString(req);
        String md5 = MD5Util.MD5(jsonStr);
        boolean flag = AppExReportMd5Redis.me().isMember(md5);
        if (flag) {
            log.warn("该异常信息已重复！ 异常信息为：{},md5值：{}", jsonStr, md5);
            return;
        }
        AppExReportMd5Redis.me().add(md5);
        AppExReport appExReport = new AppExReport();
        BeanUtil.quickCopy(req, appExReport);
        LocalDateTime now = LocalDateTime.now();
        appExReport.setTimestampCode(now.toInstant(ZoneOffset.ofHours(8)).toEpochMilli() / 1000);
        appExReport.setCreateTime(now);
        appExReport.insert();
    }
}
